/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.system.controller;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.system.domain.Announcement;
import com.koron.system.service.api.AnnouncementService;
import com.koron.system.utils.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Date;

@Api(tags = "announcement", description = "通知公告接口类")
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @ApiOperation("保存通知公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知公告id,新增不传，修改必传" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "通知公告标题", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "通知公告内容", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "beginTime", value = "播报开始时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "播报结束时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "annouIndex", value = "排序", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "url", value = "链接地址" , dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/save.htm", method = RequestMethod.POST)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore Announcement announcement) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        announcement.setUpdateUser(user == null ? null : user.getName());
        announcement.setUpdateTime(new Date());
        return announcementService.save(announcement);
    }

    @ApiOperation("逻辑删除通知公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "通知公告id,多个之间用，分开", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/batchDeteleLogic.htm", method = RequestMethod.POST)
    public MessageBean<Integer> batchDeteleLogic(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String ids) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        return announcementService.batchDeteleLogic((StringUtils.isEmpty(ids) ? null : Arrays.asList(ids.split("[,，]"))));
    }

    @ApiOperation("物理删除通知公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "通知公告id,多个之间用，分开", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/batchDetele.htm", method = RequestMethod.POST)
    public MessageBean<Integer> batchDetele(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String ids) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        return announcementService.batchDetele((StringUtils.isEmpty(ids) ? null : Arrays.asList(ids.split("[,，]"))));
    }

    @ApiOperation("通知公告详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知公告id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/details.htm", method = RequestMethod.POST)
    public MessageBean<Announcement> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam final String id) {
        if (null == user) {
            MessageBean<Announcement> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        return announcementService.queryOne(id);
    }

    @ApiOperation("通知公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "通知公告标题" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "beginTime", value = "播报开始时间" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "播报结束时间" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/list.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<Announcement>> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@RequestParam(required = false) final String title, @RequestParam(required = false) final @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginTime, @RequestParam(required = false) final @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
                                                    @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize) {
        if (null == user) {
            MessageBean<DataBean<Announcement>> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        Integer start = null;
        if (page != null) {
            if (page <= 0) {
                page = 1;
                if (pageSize == null || pageSize <= 0) {
                    pageSize = 15;
                }
            }
            start = (page <= 1) ? 0 : (page - 1) * pageSize;
        }
        MessageBean<DataBean<Announcement>> msg = announcementService.list(title, beginTime, endTime, start, pageSize);
        if (null != msg.getData()) {
            msg.getData().setPageNo(page == null ? 1 : page);
        }
        return msg;
    }
}
